<script setup lang='ts'>
import { ref, watch } from 'vue';
import DateSelect from './base/DateSelect.vue'
const props = withDefaults(defineProps<{
    modelValue?: number,
    withUndefined?: boolean,
    defaultValue?: number,
    readonly?: boolean,
    propertyName?: string
}>(), { withUndefined: true, readonly: false, propertyName: '属性值' })

const numbersRef = ref<any>()
const emits = defineEmits(["update:modelValue"]);
//时间戳转换为时间
const timestampToTime = (timestamp: number) => {
    const date = new Date(timestamp); // 根据时间戳创建Date对象
    const year = date.getFullYear(); // 获取年份
    const month = date.getMonth() + 1; // 获取月份，需要加1
    const day = date.getDate(); // 获取日期
    const hour = date.getHours(); // 获取小时
    const minute = date.getMinutes(); // 获取分钟
    const second = date.getSeconds(); // 获取秒数
    const millisecond = date.getMilliseconds(); // 获取毫秒数
    const formattedDate = `${year}-${month.toString().length === 1 ? `0${month}` : month}-${day.toString().length === 1 ? `0${day}` : day} ${hour.toString().length === 1 ? `0${hour}` : hour}:${minute.toString().length === 1 ? `0${minute}` : minute}:${second.toString().length === 1 ? `0${second}` : second}.${millisecond}`; // 拼接成格式化后的日期字符串
    return formattedDate
}
watch(() => props.modelValue, () => {
    if (!props.defaultValue) return
    numbersRef.value = timestampToTime(props.modelValue ?? props.defaultValue)
}, { immediate: true })
//时间转换为时间戳
const toTimetampTimes = (dateTime: string) => {
    const date = new Date(dateTime)
    const time = date.getTime()
    return time
}
const inputHandler = (e: any) => {
    mouseFlag.value = false
    emits('update:modelValue', toTimetampTimes(e.target.value))
}
const resetDefult = () => {
    emits('update:modelValue', props.defaultValue)
}
const mouseFlag = ref(false)
</script>

<template>
    <div style="width: 100%;display: flex;">
        <div class="data_prop">
            <input type="datetime-local"
                :style="{ width: (!readonly && defaultValue && modelValue !== defaultValue) ? '140px' : '170px' }"
                name="time" step=".1" :disabled="readonly" v-model="numbersRef" @input="inputHandler($event)">
        </div>
        <div class="checkbox1">
            <div v-if="!readonly && defaultValue && modelValue !== defaultValue" class="checkbox"
                @click.stop.prevent="resetDefult" @mouseover="mouseFlag = true" @mouseout="mouseFlag = false">
                <es-icon :name="'huaban'" :color="mouseFlag ? 'rgba(230, 230, 230, 1)' : 'rgba(230, 230, 230, 0.4)'"
                    :size="14" />
            </div>
        </div>

    </div>
</template>

<style scoped>
.data_prop {
    flex: 1;
}

.data_prop>input {
    width: 140px;
    height: 20px;
    border: 1px solid #3B3C40;
    outline: none;
    background: rgba(28, 28, 29, 0.6);
    color: rgba(230, 230, 230, 1);
    border-radius: 4px;
}

.checkbox1 {
    width: 20px;
    height: 20px;
    box-sizing: border-box;
    margin-left: 10px;
    margin-top: 2px;
}

.checkbox {
    width: 20px;
    height: 20px;
    cursor: pointer;
    border: 1px solid rgba(230, 230, 230, 0.4);
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
}
</style>
